package kercar.manager;

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;

import kercar.debug.Debugger;
import kercar.gps.GPSPosition;
import kercar.route.Route;
import kercar.xmltools.RouteRequester;

public class MapManager {

	/************************************************************************* BLOCK
	 * ATTRIBUTES
	 *******************************************************************************/
	
	private List<Route> _routes;
	private int _currentRoute;
	
	/************************************************************************* BLOCK
	 * CONSTRUCTOR
	 *******************************************************************************/
	
	public MapManager() {
		_routes = new ArrayList<>();
		_currentRoute = 0;
	}

	/************************************************************************* BLOCK
	 * PUBLIC METHODS
	 *******************************************************************************/
	
	public void addRoute(Route route) {
		_routes.add(route);
	}
	
	public Route nextRoute() {
		if(_currentRoute < _routes.size()) {
			_currentRoute++;
			return _routes.get(_currentRoute);
		}
		return null;		
	}
	
	public Route getCurrentRoute() {
		return _routes.get(_currentRoute);
	}
	
	/************************************************************************* BLOCK
	 * PUBLIC STATIC METHODS
	 *******************************************************************************/
	
	public static Route computeRoute(GPSPosition origin, GPSPosition destination) {
		Route route = new Route();
		
		List<GPSPosition> waypoints = RouteRequester.getRoute(origin, destination);
		
		for(GPSPosition gpsPosition : waypoints) {
			route.addTarget(gpsPosition);
			Debugger.logMsg(gpsPosition.toString());
		}
		
		try {
			route.loadNextTarget();
		} catch (NoSuchElementException e) {
			e.printStackTrace();
		}
		return route;
	}
	
}
